<?php

namespace Modules\Api\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Bill;
use App\Models\User;
use App\Models\UserScoreRecord;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BillController extends Controller{
    //查询账单信息
    public function userBillApi(Request $request){
        try{
            $openid = $request->input("openid",false);
            $page = $request->input("page",1);
            $size = $request->input("size",20);
            if (!$openid)return self::error();
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            //查询消费记录
            $billCount = Bill::where("userid",$user->id)->where("status",1)->count();
            $bill = Bill::where("userid",$user->id)->where("status",1)->forPage($page,$size)->orderBy('id','desc')->get();
            $userBill = [];
            if ($bill->count()>0){
                foreach ($bill as $item){
                    if ($item['type']==1){
                        $typeName = "充值";
                    }else{
                        $typeName = "消费";
                    }
                    $userBill[] = [
                        "id" => $item['id'],
                        "title" => "订单{$item['order_sn']}消费",
                        "money" => $item['money'],
                        "type" => $item['type'],
                        "type_name" => $typeName,
                        "date" => date('Y-m-d H:i:s',strtotime($item['created_at']))
                    ];
                }
            }
            //查询积分信息
            $userScoreCount = UserScoreRecord::where("user_id",$user->id)->count();
            $userScoreRecord = UserScoreRecord::where("user_id",$user->id)->forPage($page,$size)->get();
            $userScore = [];
            if ($userScoreRecord->count()>0){
                foreach ($userScoreRecord as $item){
                    if ($item->type==1){
                        $typeName = '充值';
                    }elseif ($item->type==2){
                        $typeName = '分享';
                    }else{
                        $typeName = '赠送';
                    }
                    $userScore[] = [
                        "id" => $item['id'],
                        "title" => "订单{$item['order_sn']}消费",
                        "money" => $item['score'],
                        "type" => $item,
                        "type_name" => $typeName,
                        "date" => date('Y-m-d H:i:s',strtotime($item['created_at']))
                    ];
                }
            }
            return self::success(["score"=>$user->score,"bill"=>["count"=>$billCount,"list"=>$userBill],"record"=>["count"=>$userScoreCount,"list"=>$userScore]]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }

    //充值记录
    public function billRechargeRecordsApi(Request $request){
        try{
            $openid = $request->input("openid",false);
            $page = $request->input("page",1);
            $size = $request->input("size",20);
            if (!$openid)return self::error(1000);
            $user = User::where("openid",$openid)->first();
            if (!$user)return self::error(1002);
            //查询消费记录
            $billCount = Bill::where("userid",$user->id)->where("type",1)->where("status",1)->count();
            $bill = Bill::where("userid",$user->id)->where("type",1)->where("status",1)->forPage($page,$size)->orderBy('id','desc')->get();
            $userBill = [];
            if ($bill->count()>0){
                foreach ($bill as $item){
                    if ($item['type']==1){
                        $typeName = "充值";
                    }else{
                        $typeName = "消费";
                    }
                    $userBill[] = [
                        "id" => $item['id'],
                        "title" => "订单{$item['order_sn']}充值",
                        "money" => $item['money'],
                        "type" => $item['type'],
                        "type_name" => $typeName,
                        "date" => date('Y-m-d H:i:s',strtotime($item['created_at']))
                    ];
                }
            }
            return self::success(["count"=>$billCount,"list"=>$userBill]);
        }catch (\Exception $e){
            Log::error($e);
            return self::error(500);
        }
    }
}
